package com.meitianhui.convenitenbuy.module.store.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.module.store.contract.OneLotteryDSKContract;
import com.meitianhui.convenitenbuy.module.store.http.StoreApiDataSource;
import com.meitianhui.convenitenbuy.module.store.model.DSKBean;
import com.meitianhui.convenitenbuy.module.store.model.DskStoreDetailBean;
import com.meitianhui.convenitenbuy.module.store.model.PayInfoBean;
import com.meitianhui.convenitenbuy.module.store.ui.activity.OneLotteryDSKActivity;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/27.
 */

public class OneLotteryDSKPresenter implements OneLotteryDSKContract.Presenter {

    private OneLotteryDSKActivity mContext;
    private final OneLotteryDSKContract.View mView;
    private Gson gson;

    public OneLotteryDSKPresenter(OneLotteryDSKActivity view,
                                  OneLotteryDSKActivity context) {
        this.mView = view;
        this.mContext = context;
        gson = new Gson();
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getDskInfo(String activityId) {
        Map<String, String> map = new HashMap<>();
        map.put("activity_id", activityId);
        String params = gson.toJson(map);

        StoreApiDataSource.getInstance()
                .getDskInfo(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "goods.ldActivitiesPageFind", params)
                .subscribe(new ResponseObserver<BaseResponse<DSKBean>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<DSKBean> response) {
                        mView.getDskInfoBack(true, null, response.data.getList() == null ?
                                null : response.data.getList().get(0), response.data.getDoc_url());
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.getDskInfoBack(false, msg, null, null);
                    }
                });
    }

    @Override
    public void postFavoriteStore(String storeId) {
        Map<String, String> map = new HashMap<>();
        map.put("stores_id", storeId);
        map.put("consumer_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        String params = gson.toJson(map);

        StoreApiDataSource.getInstance()
                .operationCollect(InfoProvider.getAppToken(App.getInstance()),
                        "stores.consumer.favoriteStore", params)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        mView.postFavoriteStoreBack(true, null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.postFavoriteStoreBack(false, msg);
                    }
                });
    }

    @Override
    public void checkDskInfo(String activity_id, String num) {
        UserToken token = InfoProvider.getUserToken(App.getInstance());
        Map<String, String> map = new HashMap<>();
        map.put("activity_id", activity_id);
        map.put("consumer_id", token.getMember_id());
        map.put("qty", num);
        map.put("activity_type", "DSK");
        String params = gson.toJson(map);

        StoreApiDataSource.getInstance()
                .payOneLottery(token.getUser_token(), "goods.ldActivityPayInfoFind", params)
                .subscribe(new ResponseObserver<BaseResponse<PayInfoBean>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseResponse<PayInfoBean> response) {
                        mView.checkDskInfoBack(true, null, response.data.getPay_info());
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.checkDskInfoBack(false, msg, null);
                    }
                });
    }

    @Override
    public void payDskOrder(Map<String, String> map) {
        String userToken = InfoProvider.getUserToken(App.getInstance()).getUser_token();
        String service = "finance.ldOrderPay";
        Map<String, String> map1 = new HashMap<>();
        map1.put("data_source", Constants.CUSTOM_APP);
        map1.put("payment_way_key", Constants.PAYMENT_WAY_05);
        map.putAll(map1);
        String params = gson.toJson(map);

        StoreApiDataSource.getInstance()
                .payOneLotteryOrder(userToken, service, params)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        mView.payDskOrderBack(true, null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.payDskOrderBack(false, msg);
                    }
                });
    }

    @Override
    public void getStoreDskInfo(int page, String activityId) {
        String userToken = InfoProvider.getUserToken(App.getInstance()).getUser_token();
        String service = "goods.ldActivityProcessForStorePageFind";
        Map<String, String> maps = new HashMap<>();
        maps.put("page_no", page + "");
        maps.put("page_size", "100");
        String pageParams = gson.toJson(maps);

        Map<String, String> map = new HashMap<>();
        map.put("activity_id", activityId);
        String params = gson.toJson(map);

        StoreApiDataSource.getInstance()
                .getStoreDskInfo(userToken, service, pageParams, params)
                .subscribe(new ResponseObserver<BaseResponse<DskStoreDetailBean>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<DskStoreDetailBean> response) {
                        mView.getStoreDskInfoBack(true, null, response.data.getList());
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.getStoreDskInfoBack(false, msg, null);
                    }
                });
    }

}
